package edu.software.cakehomework.controller;

import edu.software.cakehomework.entity.Type;
import edu.software.cakehomework.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/types")
@CrossOrigin(origins = "*")
public class TypeController {

    @Autowired
    private TypeService typeService;

    @GetMapping
    public ResponseEntity<?> getAllTypes() {
        try {
            List<Type> types = typeService.getAllTypes();
            return ResponseEntity.ok(types);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<?> getTypeById(@PathVariable Integer id) {
        try {
            Type type = typeService.getTypeById(id);
            return ResponseEntity.ok(type);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/category/{category}")
    public ResponseEntity<?> getTypeByCategory(@PathVariable Type.Categories category) {
        try {
            Type type = typeService.getTypeByCategory(category);
            return ResponseEntity.ok(type);
        } catch (RuntimeException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}